/**
 * 
 */
package com.example.exportdatafromweb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author ThangTB
 *
 */
public class DataHeper {
	Context mContext;
	
//	public String databasePath = "data/data/com.example.exportdatafromweb/data.sqlite";
	public String databasePath = "";
	public String databaseName = "data.sqlite";
	public SQLiteDatabase database=null;	
	public File databaseFile;
	
	String TABLE_RECIP = "tbl_recipes";
	
	/**
	 * @param context
	 */
	public DataHeper(Context context) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
		databasePath = "data/data/"+context.getPackageName()+"/data.sqlite";
		databaseFile = new File(databasePath);
		if(!databaseFile.exists())
			try {
				deployDataBase(databaseName, databasePath);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	/**
	 * 
	 * copy database to devices
	 * @param databaseName
	 * @param tagertPath
	 * @throws IOException
	 */
	private void deployDataBase(String databaseName, String tagertPath) throws IOException    {  
    	InputStream myInput = mContext.getAssets().open(databaseName);  
    	String outFileName = tagertPath;  	
    	OutputStream myOutput = new FileOutputStream(outFileName);  

    	byte[] buffer = new byte[1024]; 
    	int length; 
    	while ((length = myInput.read(buffer))>0)  
    	{  
    		myOutput.write(buffer, 0, length);   
    	}  
    	
    	myOutput.flush();  
    	myOutput.close();  
    	myInput.close();   
    }
	
	/**
	 * @author thangtb
	 * @since Mar 6, 2014 - 7:12:29 PM
	 * @param entity
	 * @return
	 */
	public boolean insertCeleb(FoodEntity entity){
		String query ="";
		query = "INSERT INTO "+TABLE_RECIP 
		+"( recipe_name, image_preview, prepare_time, cook_time, serves, summary, ingredients, directions )"
		+" VALUES('"+entity.getRecipe_name()+"','"
					+entity.getImage()+"','"
					+entity.getPrepare_time()+"','"
					+entity.getCook_time()+"','"
					+entity.getServes()+"','"
					+entity.getSummary()+"','"
					+entity.getIngredient()+"','"
					+entity.getDirections()+"'"
		+")";
//		Log.d("ThangTB", "sql: "+query);
		try {
			database = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
			database.execSQL(query);
			database.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	/**
	 * @param QuoteID
	 * @return
	 */
	public boolean DeleteAllFavourites(){
		String query="";
		query = "UPDATE quotes SET is_favourist = 0 WHERE  is_favourist = 1 ;";
		
		try {
			database = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
			database.execSQL(query);
			database.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
}
